<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace longbingcore\wxcore;

class DadaApi
{
    private $URL = "http://newopen.imdada.cn";
    private $APP_KEY = "";
    private $VERSION = "1.0";
    private $APP_SECRET = "";
    private $API_ADDORDER = "/api/order/addOrder";
    private $API_FETCHORDER = "/api/order/fetch";
    private $API_CITY_LIST = "/api/cityCode/list";
    private $API_FINISHORDER = "/api/order/finish";
    private $API_CANCELORDER = "/api/order/cancel";
    private $API_EXPIREORDER = "/api/order/expire";
    private $API_FORMALCANCEL = "/api/order/formalCancel";
    private $API_CANCELREASONS = "/api/order/cancel/reasons";
    private $API_ACCEPTORDER = "/api/order/accept";
    private $API_ADDTIP = "/api/order/addTip";
    private $API_READDORDER = "/api/order/reAddOrder";
    private $API_QUERYDELIVERFEE = "/api/order/queryDeliverFee";
    private $API_ADDAFTERQUERY = "/api/order/addAfterQuery";
    private $API_ADDSHOP = "/api/shop/add";
    private $API_ADDMERCHANT = "/merchantApi/merchant/add";
    private $API_ORDERINFO = "/api/order/status/query";
    private $SOURCE_ID = "73753";
    private $SHOP_NO = "11047059";
    private $SUCCESS = "success";
    private $FAIL = "fail";
    private $call_back;
    public function __construct($appkey, $appsec, $source_id, $shop_no)
    {
        $this->APP_KEY = $appkey;
        $this->APP_SECRET = $appsec;
        $this->SHOP_NO = $shop_no;
        $this->SOURCE_ID = $source_id;
        $this->incon = "?";
        if(longbingIsWeiqin()) {
            $params = $_GET;
            $i = $params["i"];
            $t = $params["t"];
            $v = $params["v"];
            $n = $params["m"];
            $this->call_back = "https://" . $_SERVER["HTTP_HOST"] . "/app/index.php?i=" . $i . "&t=" . $t . "&v=" . $v . "&from=wxapp&c=entry&a=wxapp&do=api&core=core2&m=" . $n . "&s=restaurant/SendResult/dadaSend";
            $this->incon = "&";
        } else {
            $this->call_back = "http://" . $_SERVER["HTTP_HOST"] . "/restaurant/SendResult/dadaSend";
            $this->incon = "?";
        }
    }
    public function addOrder($data)
    {
        $city_code = $this->cityCode($data["city"]);
        if(!empty($city_code["code"]) && $city_code["code"] == 500) {
            return $city_code;
        }
        $arr = ["shop_no" => $this->SHOP_NO, "origin_id" => $data["order_code"], "city_code" => $city_code, "is_prepay" => 0, "cargo_price" => $data["pay_price"], "receiver_name" => $data["address"]["name"], "receiver_address" => $data["address"]["address"] . $data["address"]["info"], "receiver_phone" => $data["address"]["phone"], "receiver_lat" => $data["address"]["lat"], "receiver_lng" => $data["address"]["long"], "callback" => $this->call_back . $this->incon . "id=" . $data["id"], "cargo_weight" => $data["weight"]];
        $res = self::getResult($this->API_ADDORDER, $arr);
        return $res;
    }
    public function reAddOrder($data)
    {
        $arr = ["shop_no" => $this->SHOP_NO, "origin_id" => $data["order_code"], "city_code" => $this->cityCode($data["city"]), "is_prepay" => 0, "cargo_price" => $data["pay_price"], "receiver_name" => $data["address"]["name"], "receiver_address" => $data["address"]["address"] . $data["address"]["info"], "receiver_phone" => $data["address"]["phone"], "receiver_lat" => $data["address"]["lat"], "receiver_lng" => $data["address"]["long"], "callback" => $this->call_back . $this->incon . "id=" . $data["id"], "cargo_weight" => $data["weight"]];
        return self::getResult($this->API_READDORDER, $arr);
    }
    public function queryDeliverFee($data)
    {
        $data["shop_no"] = $this->SHOP_NO;
        $arr = ["shop_no" => $this->SHOP_NO, "origin_id" => $data["id"], "city_code" => $this->cityCode($data["city"]), "is_prepay" => 0, "receiver_name" => $data["address"]["name"], "receiver_address" => $data["address"]["address"] . $data["address"]["info"], "receiver_phone" => $data["address"]["phone"], "receiver_lat" => $data["address"]["lat"], "receiver_lng" => $data["address"]["long"], "callback" => $this->call_back . $this->incon . "id=" . $data["id"], "cargo_weight" => $data["weight"]];
        return self::getResult($this->API_QUERYDELIVERFEE, $arr);
    }
    public function addAfterQuery($data)
    {
        return self::getResult($this->API_ADDAFTERQUERY, $data);
    }
    public function orderInfo($data)
    {
        $arr["order_id"] = $data["order_code"];
        return self::getResult($this->API_ORDERINFO, $arr);
    }
    public function formalCancel($data)
    {
        return self::getResult($this->API_FORMALCANCEL, $data);
    }
    public function cancelOrder($data)
    {
        return self::getResult($this->API_CANCELORDER, $data);
    }
    public function addTip($data)
    {
        return self::getResult($this->API_ADDTIP, $data);
    }
    public function addShop($data)
    {
        return self::getResult($this->API_ADDSHOP, $data);
    }
    public function addMerchant($data)
    {
        $this->SOURCE_ID = "";
        return self::getResult($this->API_ADDMERCHANT, $data);
    }
    public function cancelReasons()
    {
        $res = self::getResult($this->API_CANCELREASONS);
        var_dump($res);
    }
    public function acceptOrder($data)
    {
        return self::getResult($this->API_ACCEPTORDER, $data);
    }
    public function fetchOrder($data)
    {
        return self::getResult($this->API_FETCHORDER, $data);
    }
    public function finishOrder($data)
    {
        return self::getResult($this->API_FINISHORDER, $data);
    }
    public function expireOrder($data)
    {
        return self::getResult($this->API_EXPIREORDER, $data);
    }
    public function processCallback()
    {
        $content = file_get_contents("php://input");
        if($content) {
            $arr = json_decode($content, true);
        }
    }
    public function cityCode($city)
    {
        $code = 0;
        $data = self::getResult($this->API_CITY_LIST);
        if(empty($data["result"])) {
            return $data;
        }
        $city = str_replace("市", "", $city);
        $found_key = array_search($city, array_column($data["result"], "cityName"));
        if(!is_numeric($found_key)) {
            return ["code" => 500, "msg" => "达达暂不支持该城市"];
        }
        if(key_exists($found_key, $data["result"])) {
            $code = $data["result"][$found_key]["cityCode"];
        }
        return $code;
    }
    private function sign($param, $time)
    {
        $tmpArr = ["app_key" => $this->APP_KEY, "body" => $param, "format" => "json", "source_id" => $this->SOURCE_ID, "timestamp" => $time, "v" => $this->VERSION];
        if(empty($this->SOURCE_ID)) {
            unset($tmpArr["source_id"]);
        }
        $str = "";
        foreach ($tmpArr as $k => $v) {
            $str .= $k . $v;
        }
        $str = $this->APP_SECRET . $str . $this->APP_SECRET;
        $signature = md5($str);
        return strtoupper($signature);
    }
    private function getParam($data = "")
    {
        if(empty($data)) {
            $param = "";
        } else {
            $param = json_encode($data);
        }
        $time = time();
        $sign = self::sign($param, $time);
        $tmpArr = ["app_key" => $this->APP_KEY, "body" => $param, "format" => "json", "signature" => $sign, "source_id" => $this->SOURCE_ID, "timestamp" => $time, "v" => $this->VERSION];
        if(empty($this->SOURCE_ID)) {
            unset($tmpArr["source_id"]);
        }
        return json_encode($tmpArr);
    }
    private function getResult($api, $data = "")
    {
        $param = self::getParam($data);
        $url = $this->URL . $api;
        $res = self::http_post($url, $param);
        if($res) {
            $res = json_decode($res, true);
            return $res;
        }
        return false;
    }
    private function http_post($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if(stripos($url, "https://") !== false) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
        }
        if(is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = [];
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $header = ["Content-Type: application/json"];
        curl_setopt($oCurl, CURLOPT_HTTPHEADER, $header);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"]) == 200) {
            return $sContent;
        }
        return false;
    }
}

?>